package com.youlu.campus.service.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author : [dengbo]
 * @className : IkAnalyzerUtil
 * @description : [描述说明该类的功能]
 * @createTime : [2022/12/8 20:03]
 */
@Slf4j
public class IkAnalyzerUtil {

    public static Set<String> getSegText(String text, boolean useSmart) {
        Set<String> list = new LinkedHashSet<>();
        IKSegmenter ik = new IKSegmenter(new StringReader(text), useSmart);
        try {
            Lexeme word = null;
            while ((word = ik.next()) != null) {
                list.add(word.getLexemeText());
            }
        } catch (IOException ex) {
            log.error("getSegText error", ex);
            throw new RuntimeException(ex);
        } catch (Exception ex) {
            log.error("getSegText error", ex);
        }
        return list;
    }

    public static String getSegTextStr(String text, boolean useSmart) {
        Set<String> list = getSegText(text, useSmart);
        return list.stream().collect(Collectors.joining(" "));
    }

    public static String getAndSeqText(String text, boolean useSmart) {
        Set<String> list = getSegText(text, useSmart);
        return list.stream().collect(Collectors.joining(" "));
    }


    public static String getSegTextStr(Set<String> list) {
        return list.stream().collect(Collectors.joining(" "));
    }

    public static String getAndStr(Set<String> list) {
        return list.stream().map(item -> "\"" + item + "\"").collect(Collectors.joining(" "));
    }

    public static String getExcludeStr(Set<String> list, Set<String> exclude) {
        boolean isNotNull = CollectionUtils.isNotEmpty(exclude);
        if (isNotNull) {
            list.addAll(exclude);
        }
        return list.stream().map(item -> {
            if (isNotNull && exclude.contains(item)) {
                return "-" + item;
            }
            return item;
        }).collect(Collectors.joining(" "));
    }

    public static void main(String[] args) {
        //Set<String> ak = IkAnalyzerUtil.getSegText("重庆三峡职业", false);
        Set<String> ak = IkAnalyzerUtil.getSegText("四川的大学", true);
        log.error("ak:{}", ak);
    }
}
